<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Forward;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ForwardController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Forward(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $user = User::pluck('email', 'id')->toArray();
            $grid->column('uid','所属用户')->using($user)->label(['default' => 'primary', ]);;
            $grid->column('domain')->link(function ($value) {
                return 'http://' . $value;
            });
            $grid->column('url')->link();
            $grid->column('mode')->using([1 => '显现跳转', 0 => '隐性跳转'])->label([
                0 => 'info',
                1 => 'success',
            ]);;
            $grid->column('status')->using([1 => '正常', 0 => '待审/封禁'])->label([
                0 => 'danger',
                1 => 'success',
            ]);
            // $grid->column('data');
            // $grid->column('expired_date');
            $grid->column('created_at')->display(function ($value) {
                return date('Y-m-d', strtotime($value));
            });
            // $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Forward(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('domain');
            $show->field('title');
            $show->field('keywords');
            $show->field('description');
            $show->field('url');
            $show->field('mode');
            $show->field('status');
            $show->field('data');
            $show->field('expired_date');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Forward(), function (Form $form) {
            $form->display('id');
            $form->display('uid');
            $form->display('domain');
            $form->text('title');
            $form->text('keywords');
            $form->text('description');
            $form->text('url');
            $form->select('mode')->options([1 => '显现跳转', 0 => '隐性跳转']);
            $form->select('status')->options([1 => '正常', 0 => '待审/封禁']);
            $form->text('data');
            $form->datetime('expired_date');
        
            // $form->display('created_at');
            // $form->display('updated_at');
        });
    }
}
